#!/bin/bash
#
# 4shared.com module
# Copyright (c) 2010-2012 Plowshare team
#
# This file is part of Plowshare.
#
# Plowshare is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Plowshare is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Plowshare.  If not, see <http://www.gnu.org/licenses/>.

MODULE_PRIMEWIRE_AG_REGEXP_URL="http://\(www\.\)\?primewire\.ag/"

MODULE_PRIMEWIRE_AG_DOWNLOAD_OPTIONS=""
MODULE_PRIMEWIRE_AG_DOWNLOAD_RESUME=yes
MODULE_PRIMEWIRE_AG_DOWNLOAD_FINAL_LINK_NEEDS_COOKIE=yes

MODULE_PRIMEWIRE_AG_LIST_OPTIONS=""

primewire_ag_list() {

local URL=$1

formatarraylinks ()
{
  for (( i=0; i < ${#Links[@]}; i++ )) do
      Links[i]="${Links[i]}"$'\n'
  done
}

formatarray ()
{
  for (( i=0; i < ${#Sites[@]}; i++ )) do
      Sites[i]="${Sites[i]}"$'\n'
  done
} 

process_putlocker()
{
    finallink=$(curl -e $URL -i "${Links[${putlocker[k]}]}" | grep Location | replace 'Location: ' '')
}

process_sockshare()
{
    finallink=$(curl -e $URL -i "${Links[${sockshare[k]}]}" | grep Location | replace 'Location: ' '')
}

initarray ()
{
  for (( i=0; i < ${#Sites[@]}; i++ )) do
      Sites[i]=${Sites[i]:141}
      Sites[i]=${Sites[i]%?????????????????????????}
     # Sites[i]="${Sites[i]}"$'\n'
  done
  for (( j=0; j < ${#Links2nd[@]}; j++ )) do
      if [[ "${Links2nd[$j]}" == *"Part 2"* ]] ; then
      #  echo "$i"
       # formatarraylinks
         # Links=( ${Links[*]:0:$((i))} ${Links[*]:$((i))} )
        IFS=$'\n'
        
        
        Sites=( ${Sites[@]:0:$j} ${Sites[@]:$((j+1))} )
        
        IFS=$' \t\n'
        
        # (IFS=$'\n'; echo "${Links[*]}" > linklist01)
        
      #  formatarraylinks
       # echo ${Links[15]}
      fi
      if [[ $(echo "${Links2nd[$j]}") == *"Part 3"* ]] ; then
      #  formatarraylinks
      IFS=$'\n'
     # echo $j > j
     # echo ${#Links[@]} > linknum
      if (( "($j+1)" > "${#Links[@]}" )) ; then
           Links+="asdfasdfasdf"
      fi
        Links=( ${Links[*]:0:$((j))} ${Links[*]:$((j+1))} )
       # formatarraylinks
      # echo "${Links[*]}" > linklistinit$j
        IFS=$' \t\n'
      fi
  done
} 

stripurl=$( echo "$URL" | replace 'http://' '' )
numofslashes=${stripurl//[^\/]/}
numofslashes=${#numofslashes}

Pageinit=$(curl "$URL") || return

# echo "$Pageinit" > Pageinit

if [[ $numofslashes != "1" ]] || [[ $( echo "$Pageinit" | grep 'Season 1' ) == '' ]]; then

	Pageinit=$(curl "$URL") || return

	
	
	if [[ $(echo "$Pageinit" | grep 'external.php') = '' ]]; then
    return $ERR_LINK_DEAD
	fi
#	echo "notright" > notright

IFS=$'\n'
	Links=( $(echo "$Pageinit" | grep 'external.php' | grep -v 'Part 2' | grep -v 'Part 3' | parse_all_attr "href" ) )
	Links2nd=( $(echo "$Pageinit" | grep 'external.php' | grep -v 'Part 2' | grep -v 'Part 3' ) )
	Sites=( $(echo "$Pageinit" | grep 'document.writeln') )
	# echo "${Sites[*]}" > origsitelist.txt
IFS=$' \t\n'
	 
	# echo "$Pageinit" > Pageinit
	 
	 for (( i=0; i < ${#Sites[@]}; i++ )) do
       if match "Watch HD" "${Sites[i]}"; then
          unset Sites[i]
          unset Links[i]
          unset Links2nd[i]
       fi
   done
	
	
#	echo ${#Links2nd[@]} > links2ndraw
	
	
	#  echo "${Sites[@]}" > origsitelist.txt
  #  echo "${Links[*]}" > origlinklist.txt
  #  echo "${Links[1]}" > link1
	
 # for (( i=0; i < ${#Links[@]}; i++ )) do
    #  Links[i]=$( echo "${Links[i]}" | strip)
    #  Links[i]="http://www.primewire.ag"${Links[i]}
   #   Links[i]=$( echo "${Links[i]}" | strip)
     # Links[i]+=$'\n'
      # Links[i]=$(echo -e "${Links[i]}'\n'")
 # done
  
# (IFS=$'\n'; echo "${Links[*]}" > linklist0)
  
# (IFS=$'\n'; echo "${Sites[*]}" > sites)
 
# (IFS=$'\n'; echo "${Links2nd[*]}" > links2nd)
  
   IFS=$' \t\n'
  
 # (IFS=$'\n'; echo "${Sites[*]}" > sites00)
  
    for (( i=0; i < ${#Sites[@]}; i++ )) do
      Sites[i]=${Sites[i]:141}
      Sites[i]=${Sites[i]%?????????????????????????}
     Sites[i]="${Sites[i]}"$'\n'
  done
  
 #	initarray
 	
	for (( i=1; i < $((${#Sites[@]}+1)); i++ )) do
       if match "Watch HD" "${Sites[$((i-1))]}"; then
          if [ $i = 1 ]; then
        #  (IFS=$'\n'; echo "${Sites[*]}" > sites01)
            Sites=( ${Sites[@]:1} )
            Links=( ${Links[@]:1} )
         #   (IFS=$'\n'; echo "${Links[*]}" > linklist02)
         #   (IFS=$'\n'; echo "${Sites[*]}" > sites02)
            formatarray
            formatarraylinks
            i=0
          fi
          if [[ $i > 1 ]]; then
            IFS=$'\n'
            Sites=(${Sites[@]:0:$((i-1))} ${Sites[@]:$(($i))})
            Links=(${Links[@]:0:$((i-1))} ${Links[@]:$(($i))})
            IFS=$' \t\n'
        #    (IFS=$'\n'; echo ${Links[*]} > linklist$i)
            formatarray
            formatarraylinks
            i=0
          fi
       fi
  done
  
#   for (( i=0; i < ${#Links[@]}; i++ )) do
 #     if [[ $i == 0 ]]; then
 #           Links[i]=$( echo "${Links[i]}" | strip)
#            Links[i]+=$'\n'
#      elif [[ 0 == 0 ]]; then
#            Links[i]=$( echo "${Links[i]:2}" | strip)
#            Links[i]+=$'\n'
#      fi
#   done

 
  # Sites in order of preference zalaa.com,uploadc.com,putlocker,sockshare,filenuke.com,filebox.com,
a=0
b=0
c=0
d=0
e=0
f=0
g=0
h=0
l=0
m=0
n=0
o=0
p=0
q=0
r=0
s=0
t=0
 for (( i=0; i < ${#Sites[@]}; i++ )) do
      if [[ "${Sites[i]}" == *"veehd.com"* ]]; then
        veehd[a]=$i
        a+=1
      elif [[ "${Sites[i]}" == *"stagevu.com"* ]]; then
        stagevu[b]=$i
        b+=1
      elif [[ "${Sites[i]}" == *"zalaa.com"* ]]; then
        zalaa[c]=$i
        c+=1
      elif [[ "${Sites[i]}" == *"uploadc.com"* ]]; then
        uploadc[d]=$i
        d+=1
      elif [[ "${Sites[i]}" == *"putlocker.com"* ]]; then
        putlocker[e]=$i
        e+=1
      elif [[ "${Sites[i]}" == *"sockshare.com"* ]]; then
        sockshare[f]=$i
        f+=1
#      elif [[ "${Sites[i]}" == *"filenuke.com"* ]]; then
#        filenuke[g]=$i
#        g+=1
      elif [[ "${Sites[i]}" == *"filebox.com"* ]]; then
        filebox[h]=$i
        h+=1  
      elif [[ "${Sites[i]}" == *"daclips"* ]]; then
        daclips[l]=$i
        l+=1
      elif [[ "${Sites[i]}" == *"vidbux.com"* ]]; then
        vidbux[m]=$i
        m+=1
      elif [[ "${Sites[i]}" == *"nosvideo.com"* ]]; then
        nosvideo[n]=$i
        n+=1
      elif [[ "${Sites[i]}" == *"gorillavid.in"* ]] || [[ "${Sites[i]}" == *"gorillavid.com"* ]]; then
        gorilla[o]=$i
        o+=1
#      elif [[ "${Sites[i]}" == *"videoweed"* ]]; then
#        videoweed[p]=$i
#        p+=1    
      elif [[ "${Sites[i]}" == *"vidxden"* ]]; then
        vidxden[q]=$i
        q+=1    
      elif [[ "${Sites[i]}" == *"mightyupload.com"* ]]; then
        mightyupload[r]=$i
        r+=1    
      elif [[ "${Sites[i]}" == *"bestreams.net"* ]]; then
        bestreams[s]=$i
        s+=1    
      elif [[ "${Sites[i]}" == *"vidzi.tv"* ]]; then
        vidzi_tv[t]=$i
        t+=1    
      fi
      
  done
# echo ${bestreams[@]} > bestreams 
# echo ${#mightyupload[@]} > mightynum
# echo ${daclips[2]} > daclipsthird
#    echo ${vidbux[@]} > vidbux
#   echo "${Sites[@]}" > sitelist
# (IFS=$'\n'; echo "${Links[*]}" > linklist )
# echo "${Links[5]}" > linklist5
  #  echo "${Links[${gorilla[0]}]}" > chosenlink
  # echo ${sockshare[@]} > sockshare
  echo "${Links[@]}" > linklist
linknum=''
while [[ $linknum == '' ]]; do
  if [[ ${veehd[0]} != '' ]]; then
    for (( k=0; k < ${#veehd[@]}; k++ )) do
    finallinktemp=$(curl -L "${Links[${veehd[$k]}]}" | grep 'frame src="http' | parse_attr 'src')
    linkfinal=$(plowdown -c $finallinktemp)
      if [[ "$(echo $linkfinal)" != '' ]]; then
        linknum=${veehd[k]}
        finallink=$(echo "$finallinktemp")
        break
      fi
    done
    unset veehd[@]
  elif [[ ${vidzi_tv[0]} != '' ]]; then
    for (( k=0; k < ${#vidzi_tv[@]}; k++ )) do
    finallinktemp=$(curl -L "${Links[${vidzi_tv[$k]}]}" | grep 'frame src="http' | parse_attr 'src')
    linkfinal=$(plowdown -c $finallinktemp)
      if [[ "$(echo $linkfinal)" != '' ]]; then
        linknum=${vidzi_tv[k]}
        break
      fi
    done
    unset vidzi_tv[@]
  elif [[ ${nosvideo[0]} != '' ]]; then
    for (( k=0; k < ${#nosvideo[@]}; k++ )) do
    finallinktemp=$(curl -L "${Links[${nosvideo[$k]}]}" | grep 'frame src="http' | parse_attr 'src')
    linkfinal=$(plowdown -c $finallinktemp)
      if [[ "$(echo $linkfinal)" != '' ]]; then
        linknum=${nosvideo[k]}
        break
      fi
    done
    unset nosvideo[@]
  elif [[ ${stagevu[0]} != '' ]]; then
    for (( k=0; k < ${#stagevu[@]}; k++ )) do
    finallinktemp=$(curl -L "${Links[${stagevu[$k]}]}" | grep 'frame src="http' | parse_attr 'src')
    linkfinal=$(plowdown -c $finallinktemp)
      if [[ "$(echo $linkfinal)" != '' ]]; then
        linknum=${stagevu[k]}
        finallink=$(echo "$finallinktemp")
        break
      fi
    done
    unset stagevu[@]
  elif [[ ${bestreams[0]} != '' ]]; then
    for (( k=0; k < ${#bestreams[@]}; k++ )) do
    finallinktemp=$(curl -L "${Links[${bestreams[$k]}]}" | grep 'frame src="http' | parse_attr 'src')
    linkfinal=$(plowdown -c $finallinktemp)
      if [[ "$(echo $linkfinal)" != '' ]]; then
        linknum=${bestreams[k]}
        finallink=$(echo "$finallinktemp")
        break
      fi
    done
    unset bestreams[@]
  elif [[ ${uploadc[0]} != '' ]]; then
    for (( k=0; k < ${#uploadc[@]}; k++ )) do
    finallinktemp=$(curl -L "${Links[${uploadc[$k]}]}" | grep 'frame src="http' | parse_attr 'src')
    linkfinal=$(plowdown -c $finallinktemp)
      if [[ "$(echo $linkfinal)" != '' ]]; then
        linknum=${uploadc[k]}
        finallink=$(echo "$finallinktemp")
        break
      fi
    done
    unset uploadc[@]
  elif [[ ${zalaa[0]} != '' ]]; then
    for (( k=0; k < ${#zalaa[@]}; k++ )) do
    finallinktemp=$(curl -L "${Links[${zalaa[$k]}]}" | grep 'frame src="http' | parse_attr 'src')
    linkfinal=$(plowdown -c $finallinktemp)
      if [[ "$(echo $linkfinal)" != '' ]]; then
        linknum=${zalaa[k]}
        finallink=$(echo "$finallinktemp")
        break
      fi
    done
    unset zalaa[@]
elif [[ ${sockshare[0]} != '' ]]; then
    for (( k=0; k < ${#sockshare[@]}; k++ )) do
      process_sockshare
      linkfinal=$(plowdown -c "$finallink")
      if [[ "$(echo $linkfinal)" != '' ]]; then
        linknum=${sockshare[k]}
        socksharedwn=$linknum
        break
      fi
      linkfinal=''
    done
    unset sockshare[@]
  elif [[ ${putlocker[0]} != '' ]]; then
    for (( k=0; k < ${#putlocker[@]}; k++ )) do
      process_putlocker
     # linkfinal=$(plowdown -c $(curl -L "${Links[${putlocker[$k]}]}" | grep 'frame src="http' | parse_attr 'src'))
      linkfinal=$(plowdown -c "$finallink")
      if [[ "$(echo $linkfinal)" != '' ]]; then
        linknum=${putlocker[k]}
        putlockerdwn=$linknum
        break
      fi
      linkfinal=''
    done
    unset putlocker[@]
# elif [[ ${filenuke[0]} != '' ]]; then
#    for (( k=0; k < ${#filenuke[@]}; k++ )) do
#    finallinktemp=$(curl -L "${Links[${filenuke[$k]}]}" | grep 'frame src="http' | parse_attr 'src')
#    linkfinal=$(plowdown -c $finallinktemp)
#      if [[ "$(echo $linkfinal)" != '' ]]; then
#        linknum=${filenuke[k]}
#        finallink=$(echo "$finallinktemp")
#        break
#      fi
#    done
#    unset filenuke[@]
  elif [[ ${filebox[0]} != '' ]]; then
    for (( k=0; k < ${#filebox[@]}; k++ )) do
    linkfinal=$(plowdown -c $(curl -L "${Links[${filebox[$k]}]}" | grep 'frame src="http' | parse_attr 'src'))
      if [[ "$(echo $linkfinal)" != '' ]]; then
        linknum=${filebox[k]}
        finallink=$(echo "$finallinktemp")
        break
      fi
    done
    unset filebox[@]
#  elif [[ ${videoweed[0]} != '' ]]; then
#    for (( k=0; k < ${#videoweed[@]}; k++ )) do
#    finallinktemp=$(curl -L "${Links[${videoweed[$k]}]}" | grep 'frame src="http' | parse_attr 'src')
#     linkfinal=$(plowdown -c $finallinktemp)
#      if [[ "$(echo $linkfinal)" != '' ]]; then
#        linknum=${videoweed[k]}
#        finallink=$(echo "$finallinktemp")
#        break
#      fi
#    done
#    unset videoweed[@]
  elif [[ ${daclips[0]} != '' ]]; then
    for (( k=0; k < ${#daclips[@]}; k++ )) do
     finallinktemp=$(curl -L "${Links[${daclips[$k]}]}" | grep 'frame src="http' | parse_attr 'src' | head -n 1 | strip | replace '.com' '.in')
     linkfinal=$(plowdown -c $finallinktemp )
      if [[ "$(echo $linkfinal)" != '' ]]; then
        linknum=${daclips[k]}
        finallink=$(echo "$finallinktemp")
        break
      fi
    done
    unset daclips[@]
  elif [[ ${gorilla[0]} != '' ]]; then
    for (( k=0; k < ${#gorilla[@]}; k++ )) do
    finallinktemp=$(curl -L "${Links[${gorilla[$k]}]}" | grep 'frame src="http' | parse_attr 'src' | head -n 1 | strip | replace '.com' '.in')
     linkfinal=$(plowdown -c $finallinktemp)
      if [[ "$(echo $linkfinal)" != '' ]]; then
        linknum=${gorilla[k]}
        finallink=$(echo "$finallinktemp")
        break
      fi
    done
    unset gorilla[@]
elif [[ ${mightyupload[0]} != '' ]]; then
    for (( k=0; k < ${#mightyupload[@]}; k++ )) do
    finallinktemp=$(curl -L "${Links[${mightyupload[$k]}]}" | grep 'frame src="http' | parse_attr 'src')
    linkfinal=$(plowdown -c $finallinktemp) 
      if [[ "$(echo $linkfinal)" != '' ]]; then
        linknum=${mightyupload[k]}
        finallink=$(echo "$finallinktemp")
        break
      fi
    done
    unset mightyupload[@]
elif [[ ${vidbux[0]} != '' ]]; then
    for (( k=0; k < ${#vidbux[@]}; k++ )) do
    finallinktemp=$(curl -L "${Links[${vidbux[$k]}]}" | grep 'frame src="http' | parse_attr 'src')
     linkfinal=$(plowdown -c $finallinktemp)
      if [[ "$(echo $linkfinal)" != '' ]]; then
        linknum=(${vidbux[k]})
        break
      fi
    done
    unset vidbux[@]
  elif [[ ${vidxden[0]} != '' ]]; then
    for (( k=0; k < ${#vidxden[@]}; k++ )) do
    finallinktemp=$(curl -L "${Links[${vidxden[$k]}]}" | grep 'frame src="http' | parse_attr 'src')
     linkfinal=$(plowdown -c $finallinktemp)
      if [[ "$(echo $linkfinal)" != '' ]]; then
        linknum=(${vidxden[k]})
        break
      fi
    done
    unset vidxden[@]
  elif [[ 0 == 0 ]]; then
    return $ERR_LINK_DEAD
    break
  fi  
done

# echo "linkchosen" > linkchosen

    FILE_URL=$(echo "$finallinktemp" | strip)
  
     
    # echo "$linknum" > linknum
    # echo "${daclips[@]}" > zalaa0
    # echo "linknum=$linknum putlocker=$putlocker sockshare=$sockshare"
	
	echo "$FILE_URL"
	
else

  # stripurl=$( echo $URL | replace 'http://www.primewire.ag' '')
  # pagenum=$(echo $stripurl | sed 's/[^0-9]//g' )
  pagenum=${URL:27}
  # echo $pagenum > pnum
  pagenum=$(echo $pagenum | sed -n 's/-.*//p')
  
  Sites[i]=${Sites[i]:141}
      Sites[i]=${Sites[i]%?????????????????????????}
  
  # echo "$URL" > right
  
  IFS=$'\n'
  Sitelist=($(curl $URL | grep tv-$pagenum ))
  formatsitelist ()
  {
    for (( i=0; i < ${#Sitelist[@]}; i++ )) do
        Sitelist[i]+=$'\n'
    done
  }
  for (( j=1; j < ${#Sitelist[@]}+1; j++ )) do
      if [[ $(echo "${Sitelist[$((j-1))]}" | grep '<h2>' ) != '' ]] || [[ $(echo "${Sitelist[$((j-1))]}" | grep 'transp2' ) != '' ]]; then
        if [[ $j == 1 ]]; then
          while [[ $(echo "${Sitelist[$((j-1))]}" | grep '<h2>' ) != '' ]] || [[ $(echo "${Sitelist[$((j-1))]}" | grep 'transp2' ) != '' ]]; do
            Sitelist=( ${Sitelist[@]:1} )
          done
        else
          while [[ $(echo "${Sitelist[$((j-1))]}" | grep '<h2>' ) != '' ]] || [[ $(echo "${Sitelist[$((j-1))]}" | grep 'transp2' ) != '' ]]; do
            formatsitelist
            Sitelist=(${Sitelist[@]:0:$((j-1))} ${Sitelist[@]:$(($j))})
          done
      fi
    fi
  done
  for (( j=2; j < ${#Sitelist[@]}; j++ )) do
    # test "${Sitelist[j]}" || continue
    FILE_URL=$(echo "${Sitelist[j]}")
    FILE_URL=$(echo "${Sitelist[j]}" | parse_attr "a" "href")
    FILE_URL=http://www.primewire.ag$FILE_URL
   # echo $pagenum > pagenum
  #  if [[ $(echo $FILE_URL | grep 'tv-$pagenum' ) != '' ]]; then
        echo "$FILE_URL"
  #  fi
  done
fi

unset MODULE_PRIMEWIRE_AG_LIST_OPTIONS
unset MODULE_PRIMEWIRE_AG_DOWNLOAD_FINAL_LINK_NEEDS_COOKIE
unset MODULE_PRIMEWIRE_AG_DOWNLOAD_RESUME
unset MODULE_PRIMEWIRE_AG_DOWNLOAD_OPTIONS
unset MODULE_PRIMEWIRE_AG_REGEXP_URL
unset Pageinit
unset Sitelist[@]
unset FILE_URL
unset pagenum
unset wastebin
unset linknum
unset stripurl
unset numofslashes
unset finallink
unset linkfinal
unset Links[@]
unset Links2nd[@]
unset Sites[@]
unset veehd[@]
unset putlocker[@]
unset sockshare[@]
unset gorilla[@]
unset daclips[@]
unset zalaa[@]
unset filebox[@]
unset filenuke[@]
unset uploadc[@]
unset daclips[@]
unset stagevu[@]
unset vidbux[@]
unset vidxden[@]
unset nosvideo[@]
unset mightyupload[@]
unset videoweed[@]
unset bestreams[@]
unset vidzi_tv[@]
unset putlockerdwn
unset socksharedwn
unset finallinktemp
unset URL
unset a
unset b
unset c
unset d
unset e
unset f
unset g
unset h
unset i
unset j
unset k
unset l
unset m
unset n
unset o
unset p
unset q
unset r
unset s
unset t
}
